package com.supermap.advisoryinfo.dao;

import com.supermap.advisoryinfo.entity.AdvComment;
import com.supermap.advisoryinfo.entity.Advisoryinfo;
import com.supermap.advisoryinfo.entity.Advpraise;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/**
 * @Author:langhongshuai
 * @Description:咨询数据访问层
 * @Date:18/1/28 下午11:57
 */
@Mapper
public interface AdvisoryinfoDao {
    /**
     * 获得用户的咨询信息
     * @return
     */
    @Select("select * from t_advisoryinfo t1 \n" +
            "inner join t_user t2 on t1.userId=t2.id\n"+
            " where IF(ISNULL(#{userId}),true,userId=#{userId}) and IF(ISNULL(#{areaCode}),true,t1.areacode like #{areaCode})")
    public List<Advisoryinfo> getAdvisoryInfoByUIdArea(@Param("userId")String userId,@Param("areaCode")String areaCode);

    /**
     *  增加咨询
     * @param advisoryinfo
     */
    @Insert("Insert into t_advisoryinfo(userId,areacode,question,subjectid,farmlandid,lat,lon,img_url) \n" +
            "values(#{Advisory.userId},#{Advisory.areacode},#{Advisory.question}\n" +
            ",#{Advisory.subjectid},#{Advisory.farmlandid},#{Advisory.lat},#{Advisory.lon},#{Advisory.imgUrl})")
    public void addAdvisory(@Param("Advisory")Advisoryinfo advisoryinfo);

    /**
     * 查询所有咨询
     * @return
     */
    @Select("select * from t_advisoryinfo t1 " +
            "inner join t_user t2 on t1.userId=t2.id")
    List<Advisoryinfo> getAdvisoryInfo();

    /**
     * 根据咨询ID 得到对应的点赞数
     * @param advInfoId
     * @return
     */
    @Select("select COUNT(*) from t_advpraise where advInfoId=#{advInfoId}")
    int getAdvpraiseCount(@Param("advInfoId")int advInfoId);

    /**
     * 根据userid和agrInfoId判断改用用户对改咨询点过赞
     * @param userId
     * @param advInfoId
     * @return
     */
    @Select("select isPraise from t_advpraise  where userId=#{userId} and advInfoId=#{advInfoId}")
    String  getAdvpraise(@Param("userId")int userId,@Param("advInfoId")int advInfoId);

    @Delete("delete from t_advpraise where  userId=#{userId} and advInfoId=#{advInfoId}")
    int DelAdvById(@Param("userId")int userId,@Param("advInfoId")int advInfoId);

    /**
     * 点击赞 增加一条数据
     * @param advpraise
     * @return
     */
    @Insert("Insert into t_advpraise(advInfoId,isPraise,userId) "+"values(#{advpraise.advInfoId},#{advpraise.isPraise},#{advpraise.userId}"+")")
    int addAdvpraise(@Param("advpraise")Advpraise advpraise);

    /**
     * 根据咨询ID得到对应所有评论
     * @param advInfoId
     * @return
     */
    @Select("select t1.*,t2.username from t_advcomment t1 inner JOIN t_user t2 on t2.id=t1.userId where t1.advInfoId=#{advInfoId}")
    List<AdvComment> getAdvcommentListByAdvInfoId(@Param("advInfoId")int advInfoId);

    /**
     * 增添评论
     * @param advComment
     * @return
     */
    @Insert("Insert into t_advcomment(advInfoId,comment,parentId,userId) "+"values(#{advComment.advInfoId},#{advComment.comment},#{advComment.parentId},#{advComment.userId}"+")")
    int addAdvcomment(@Param("advComment")AdvComment advComment);

    /**
     * 把未读的评论添加到表中
     * @param userid
     * @param typeid
     * @param type
     * @param comment
     * @param state
     * @return
     */
    @Insert("Insert into t_newmessage(userid,typeid,type,comment,state,uid) "
    +"values(#{userid},#{typeid},#{type},#{comment},#{state},#{uid})")
    int addNewMessage(@Param("userid")int userid, @Param("typeid")int typeid, @Param("type")int type, @Param("comment")String comment, @Param("state")int state,@Param("uid")String uid);

    /**
     * 删除咨询
     * @param advId
     */
    @Delete("Delete from t_advisoryinfo where id=#{advId}")
    public void delAdvisoryinfo(@Param("advId")int advId);

    /**
     * areacode和cropid得到咨询
     * @param areacode
     * @param subjectid
     * @return
     */
    @Select("select * from t_advisoryinfo where areacode=#{areacode} and subjectid=#{subjectid} ")
   List<Advisoryinfo>  getAdvinfoByAreacodeandCropid(@Param("areacode")String  areacode,@Param("subjectid")String  subjectid);
}
